package com.ihd.ihardware.view.tzc.me.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.databinding.ObservableArrayList;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ItemConcernBinding;
import com.ihd.ihardware.databinding.ItemConcernedBinding;
import com.ihd.ihardware.pojo.RecommendRes;
import com.ihd.ihardware.view.tzc.me.model.ConcernRepository;

import java.util.List;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;

public class ConcernViewModel extends AndroidViewModel {

    private ConcernRepository mConcernRepository;

    public ConcernViewModel(@NonNull Application application) {
        super(application);
    }

    private ObservableArrayList<RecommendRes.DataBean> mTestDetails = new ObservableArrayList<>();
    public CommonAdapter mCommonAdapter = new CommonAdapter<RecommendRes.DataBean, ItemConcernBinding>(R.layout.item_concern, mTestDetails, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
        }
    }) {
        @Override
        public void convert(ItemConcernBinding mDataBinding, RecommendRes.DataBean test_concern, int position) {
            if (!TextUtils.isEmpty(test_concern.getAvatar()))
                Glide.with(mDataBinding.concernItemLL.getContext())
                        .load(Uri.parse(test_concern.getAvatar()))
                        .apply(RequestOptions.circleCropTransform())
                        .into(mDataBinding.head);
            if (test_concern.isConcern()) {
                mDataBinding.concernBtn.setBackgroundResource(R.drawable.corners_5661f3);
                mDataBinding.concernIV.setVisibility(View.GONE);
                mDataBinding.concernTV.setText("已关注");
                mDataBinding.concernTV.setTextColor(mDataBinding.concernTV.getContext().getResources().getColor(R.color.C_FFFFFF));
            } else {
                mDataBinding.concernBtn.setBackgroundResource(R.drawable.corners_line_bg);
                mDataBinding.concernIV.setVisibility(View.VISIBLE);
                mDataBinding.concernTV.setText("关注");
                mDataBinding.concernTV.setTextColor(mDataBinding.concernTV.getContext().getResources().getColor(R.color.C_5661F3));
            }

            mDataBinding.concernBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (test_concern.isConcern())
                        mConcernRepository.delConcern(test_concern.getUserId());
                    else
                        mConcernRepository.concern(test_concern.getUserId());
                }
            });
        }
    };

    public void getConcernList() {
        if (mConcernRepository == null)
            mConcernRepository = new ConcernRepository();
        mConcernRepository.getConcernList();
    }

    public void setConcernList(List<RecommendRes.DataBean> list) {
        mTestDetails.clear();
        if (list != null && list.size() > 0)
            mTestDetails.addAll(list);
    }


    private ObservableArrayList<RecommendRes.DataBean> mConcerned = new ObservableArrayList<>();
    public CommonAdapter mConcernedAdapter = new CommonAdapter<RecommendRes.DataBean, ItemConcernedBinding>(R.layout.item_concerned, mConcerned, new CommonAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
        }
    }) {
        @Override
        public void convert(ItemConcernedBinding mDataBinding, RecommendRes.DataBean test_concern, int position) {
            if (!TextUtils.isEmpty(test_concern.getAvatar()))
                Glide.with(mDataBinding.concernedItemLL.getContext())
                        .load(Uri.parse(test_concern.getAvatar()))
                        .apply(RequestOptions.circleCropTransform())
                        .into(mDataBinding.head);
            if (test_concern.isConcern()) {
                mDataBinding.concernTV.setBackgroundResource(R.drawable.corners_80e15a);
                mDataBinding.concernTV.setText("互相关注");
            } else {
                mDataBinding.concernTV.setBackgroundResource(R.drawable.corners_5661f3);
                mDataBinding.concernTV.setText("已关注");
            }

            mDataBinding.concernTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mConcernRepository.delConcern(test_concern.getUserId());
                }
            });
        }
    };

    public void getConcernedList() {
        if (mConcernRepository == null)
            mConcernRepository = new ConcernRepository();
        mConcernRepository.getConcernedList();
    }

    public void setConcernedList(List<RecommendRes.DataBean> list) {
        mConcerned.clear();
        if (list != null && list.size() > 0)
            mConcerned.addAll(list);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (null != mConcernRepository) {
            mConcernRepository.onDestroy();
            mConcernRepository = null;
        }
    }
}
